package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.MessageActivity;
import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Bean.Down_MyFocus;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_UserFans extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_MyFocus.FansBean> list;
    private ApiModel model = new ApiModel();

    public Adapter_UserFans(Context context) {
        this.context = context;
    }

    public void setList(List<Down_MyFocus.FansBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_focus, parent, false);
        Adapter_UserFans.ViewHolder viewHolder = new Adapter_UserFans.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_MyFocus.FansBean bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_UserFans.ViewHolder viewHolder = (Adapter_UserFans.ViewHolder) holder;
        viewHolder.tvFocusUsername.setText(bean.getUser_name());
        if (bean.getUser_gender() == 1) {
            viewHolder.imgFocusGender.setImageResource(R.drawable.ic_man);
        } else {
            viewHolder.imgFocusGender.setImageResource(R.drawable.ic_woman);
        }
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                .into(viewHolder.imgFocusHeadimage);

        viewHolder.imgFocusHeadimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id", bean.getUser_id());
                context.startActivity(intent);
            }
        });

        viewHolder.btnFocusMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MessageActivity.class);
                intent.putExtra("sendid",bean.getUser_id());
                intent.putExtra("sendname",bean.getUser_name());
                context.startActivity(intent);
            }
        });

        RetrofitListener<Down_Universe> isfocuslistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if(listener.getMessage().equals("success")){
                    viewHolder.btnFocusFocus.setText("取消关注");
                }else{
                    viewHolder.btnFocusFocus.setText("关注");
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> focuslistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if(listener.getMessage().equals("success")){
                    viewHolder.btnFocusFocus.setText("取消关注");
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> unfocuslistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if(listener.getMessage().equals("success")){
                    viewHolder.btnFocusFocus.setText("关注");
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        model.isfocususer(BaseActivity.getUserinfo().getUserid(),bean.getUser_id(),isfocuslistener);

        viewHolder.btnFocusFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewHolder.btnFocusFocus.getText().toString().equals("关注")){
                    model.focususer(BaseActivity.getUserinfo().getUserid(),bean.getUser_id(),focuslistener);
                }else{
                    model.unfocususer(BaseActivity.getUserinfo().getUserid(),bean.getUser_id(),unfocuslistener);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_focus_headimage)
        MyCircleImageView imgFocusHeadimage;
        @BindView(R.id.tv_focus_username)
        TextView tvFocusUsername;
        @BindView(R.id.img_focus_gender)
        ImageView imgFocusGender;
        @BindView(R.id.btn_focus_msg)
        Button btnFocusMsg;
        @BindView(R.id.btn_focus_focus)
        Button btnFocusFocus;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
